package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.service.MyService;
import com.dlwx.parttimetalent.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 闪屏（启动页）
 */
public class SplashActivity extends BaseActivity {
    @BindView(R.id.iv_entry)
    ImageView mIVEntry;
    private static final int ANIM_TIME = 3000;
    private static final float SCALE_END = 1.20F;
    @BindView(R.id.btn_jump)
    TextView btnJump;
    private Handler mhandler = new Handler();
    private Runnable mRunable;
    private boolean isMember;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //跳转到首页
        mRunable = new Runnable() {
            @Override
            public void run() {
                boolean isLogin = (boolean) SPUtils.get(SplashActivity.this, Constant.isLogin, false);
                if (isLogin) {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                } else {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                }
                SplashActivity.this.finish();
            }
        };
        // 判断是否是第一次开启应用
        boolean isFirstOpen = (boolean) SPUtils.get(SplashActivity.this, Constant.First_START, true);
        isFirstOpen=false;
        // 如果是第一次启动，则先进入功能引导页
        if (isFirstOpen) {
            Intent intent = new Intent(this, GuideActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        // 如果不是第一次启动app，则正常显示启动屏
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        mhandler.postDelayed(mRunable, 2000);
        //startMainActivity();
        userId= (int) SPUtils.get(this,Constant.USER_ID,0);
        Intent i=new Intent(this, MyService.class);
        startService(i);
//        if(userId!=0){
//            HxUtils.login(userId+"");
//        }
    }


    /**
     * 屏蔽物理返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.btn_jump)
    public void onViewClicked() {
        mhandler.removeCallbacks(mRunable);
        boolean isLogin = (boolean) SPUtils.get(SplashActivity.this, Constant.isLogin, false);
        if (isLogin) {
            if(isMember){
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }else{
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
        } else {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        }
        SplashActivity.this.finish();
    }
    //---------------------------------------------------------
}
